package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ChatService;
import com.zhishiku.filesystem.feign.ChatFeign;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ChatServiceImplCopy implements ChatService {

    @Resource
    private ChatFeign chatFeign;

    @Value("${ai.your_api_key}")
    private String apiKey;

    @Value("${ai.chat_id}")
    private String chatId;

    @Override
    public TableDataInfo sendCompletion(Map<String, Object> request) {
        //将前端控制返回方式的转换成api数据形式
        boolean isStream  = Optional.ofNullable(request.get("stream")).map(stream-> "true".equals(stream)).orElse(false);
        request.put("stream", isStream);
        Map<String, Object> resultMap = chatFeign.sendCompletion(chatId, apiKey, request);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("执行成功");
        rspData.setData(resultMap);
        return rspData;
    }

    @Override
    public TableDataInfo createSession(Map<String, Object> requestBody) {
        Long userId = SecurityUtils.getUserId();
        requestBody.put("user_id", userId.toString());
        Map<String, Object> resultMap = chatFeign.createSession(chatId, apiKey, requestBody);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("执行成功");
        rspData.setData(resultMap);
        return rspData;
    }

    @Override
    public TableDataInfo updateSession(String sessionId, Map<String, Object> requestBody) {
        Long userId = SecurityUtils.getUserId();
        requestBody.put("user_id", userId.toString());
        Map<String, Object> resultMap = chatFeign.updateSession(chatId, apiKey, sessionId, requestBody);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("执行成功");
        rspData.setData(resultMap);
        return rspData;
    }

    @Override
    public TableDataInfo getSessions(Map<String, Object> requestBody) {

        Long userId = SecurityUtils.getUserId();

        Map<String, Object> resultMap = chatFeign.getSessions(chatId, apiKey, userId.toString());

        String require = Optional.ofNullable(requestBody.get("require")).map(String::valueOf).orElse("");
        if (require.equals("list")) { //调整出简化列表
            List<Map<String, Object>> simplifiedData = new ArrayList<>();
            for (Map<String, Object> session : (List<Map<String, Object>>) resultMap.get("data")) {
                Map<String, Object> simplifiedSession = new HashMap<>();
                simplifiedSession.put("id", session.get("id"));
                simplifiedSession.put("name", session.get("name"));
                simplifiedData.add(simplifiedSession);
            }
            resultMap.put("data", simplifiedData);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("执行成功");
        rspData.setData(resultMap);
        return rspData;
    }

    @Override
    public TableDataInfo getSession(Map<String, Object> requestBody) {

        Assert.isTrue(requestBody.containsKey("sessionId"), "sessionId不能为空");
        Long userId = SecurityUtils.getUserId();

        Map<String, Object> resultMap = chatFeign.getSession(chatId, apiKey, requestBody.get("sessionId").toString(),userId.toString());

        String require = Optional.ofNullable(requestBody.get("require")).map(String::valueOf).orElse("");
        if (require.equals("list")) { //调整出简化列表
            List<Map<String, Object>> simplifiedData = new ArrayList<>();
            for (Map<String, Object> session : (List<Map<String, Object>>) resultMap.get("data")) {
                Map<String, Object> simplifiedSession = new HashMap<>();
                simplifiedSession.put("id", session.get("id"));
                simplifiedSession.put("name", session.get("name"));
                simplifiedData.add(simplifiedSession);
            }
            resultMap.put("data", simplifiedData);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("执行成功");
        rspData.setData(resultMap);
        return rspData;
    }

    @Override
    public TableDataInfo deleteSessions(Map<String, Object> requestBody) {
        //将前端string数据类型转换成id列表
        String[] ids = requestBody.get("ids").toString().split(",");
        requestBody.put("ids", ids);
        Long userId = SecurityUtils.getUserId();
        requestBody.put("user_id", userId.toString());
        //删除session
        Map<String, Object> resultMap = chatFeign.deleteSessions(chatId, apiKey, requestBody);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("执行成功");
        rspData.setData(resultMap);
        return rspData;
    }

    @Override
    public TableDataInfo generateChart(Map<String, Object> chartConfig) {
        return null;
    }
}
